﻿@model IEnumerable<Citi.Scheduler.WebUi.Models.RoutineBaseParameterModel>
<script type="text/javascript">
    $(function () {
        $("[rel=tooltip]").tooltip({ placement: 'right' });
        $(".parameterValues").each(function (index, item) {
            validateParameter(index);
        });
    });

    function validateParameter(id) {
        $.ajax({
            url: '@Url.Action("ValidateParameter")',
            type: "post",
            data: { routineId: $("#routineId" + id).val(), value: $("#value" + id).val(), index: id },
            success: function (json) {
                if (json.validated) {
                    $("#error" + id).hide();
                    $("#ok" + id).show();
                }
                else {
                    $("#error" + id).show();
                    $("#ok" + id).hide();
                }
            }
        });
    }

    function submitParameterForm() {
        if ($(".error:visible").length > 0) {
            alert("One or more parameters are invalid. Please check them again");
            return;
        }

        $.each($(".parameterValues"), function (i, item) {
            item.value = item.value.replace(',', '||&||');
        });

        $('#submitParameter').click();
    }
</script>
@if (Model == null)
{
    <div>
        There's no parameter to this routine</div>
    return;
}
<div style="height: 250px;overflow:auto;width:97%">
    @using (Html.BeginForm("Save", "RoutineParameter"))
    {
        <table class="table table-bordered table-striped" style="width: 90% !important;">
            <tr>
                <th>
                    Name
                </th>
                <th colspan="2">
                    Value
                </th>
                <th>
                    Type
                </th>
                <th>
                    Required
                </th>
                <th>
                    Nullable
                </th>
            </tr>
            @{
        var line = 0;
        foreach (var item in Model)
        {
                <tr>
                    <td id="@string.Concat("tdName", line)">@* onmouseover="@string.Format("javascript:showHideDescription('{0}')", line)">*@
                        <span rel="tooltip" title="@(string.Format("Description: {0}", item.Description))">@item.Name</span>
                    </td>
                    <td style="display: inline;">
                        @Html.TextBoxFor(modelItem => item.Value, new
                            {
                                maxlength = 1024,
                                id = string.Concat("value", line),
                                name = string.Concat("value", line),
                                @class = "parameterValues",
                                onblur = string.Format("javascript:validateParameter('{0}')", line)
                            })
                    </td>
                    <td>
                        <img alt="Error" class="error" id="@string.Concat("error", line)" style="display: none" src="@Url.Content("~/Content/img/icons/cross.png")" />
                        <img alt="Ok" id="@string.Concat("ok", line)" style="display: none" src="@Url.Content("~/Content/img/icons/tick-circle.png")" />
                    </td>
                    <td>
                        @item.Type.Name
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Required)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Nullable)
                        @Html.HiddenFor(modelItem => item.RoutineId, new { id = string.Concat("routineId", line) })
                    </td>
                </tr>
@*<div style="display: none; border:1px solid lightblue" id="@string.Concat("description", line)" onmouseout="@string.Format("javascript:showHideDescription('{0}')", line)">
            @Html.DisplayFor(modelItem => item.Description)
        </div>*@
                <script type="text/javascript"> </script>
            line++;
        }
            }
        </table>
        <input type="submit" style="display: none;" id="submitParameter" />
    }
</div>

<div>
    <h6>
        Predefined Variables</h6>
    <div style="overflow: auto; height: 130px;width:97%">
        <table class="table table-bordered table-striped" style="width: 90% !important">
            <tr>
                <th>
                    Tag
                </th>
                <th>
                    Value
                </th>
            </tr>
            @foreach (var item in new Citi.Scheduler.Domain.RoutineParameter().Variables)
            {
                <tr>
                    <td style="width: 100px">@item.Key
                    </td>
                    <td>@item.Value
                    </td>
                </tr>
            }
        </table>
    </div>
</div>
